﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.CopsBanditsUI {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import Events.*;
    import Display.DialogUI.*;
    import org.aswing.*;
    import Classes.*;
    import Classes.util.*;
    import Classes.sim.*;
    import org.aswing.geom.*;
    import Modules.bandits.*;
    import ZLocalization.*;
    import Display.*;
    import flash.text.*;
    import Display.aswingui.*;
    import L10n.*;
    import Modules.bandits.transactions.*;

    public class UpgradesCell extends DataItemCell {

        public static const CELL_WIDTH:int = 147;
        public static const CELL_HEIGHT:int = 340;

        private var m_upgrade:UpgradeDefinition;
        private var m_bg:DisplayObject;
        private var m_position:int;
        private var m_dummy:Boolean = false;
        private var m_headerPanel:JPanel;
        private var m_rewardPanel:JPanel;
        private var m_claimPanel:JPanel;
        protected var m_metRequirements:Boolean = false;
        protected var m_iconLoader:Loader;
        protected var m_icon:DisplayObject;
        protected var m_iconHolder:JPanel;
        protected var m_rewardLoader:Loader;
        protected var m_rewardIcon:DisplayObject;
        protected var m_rewardHolder:JPanel;
        protected var m_numAssetLoads:int = 0;
        protected var m_titlePanel:AssetPane;
        protected var m_upgradeButton:CustomButton;
        protected var m_curLevel:int;
        protected var m_requiredLevel:int;

        public function UpgradesCell(_arg1:int){
            this.m_position = _arg1;
            super(new SoftBoxLayout(SoftBoxLayout.Y_AXIS, 0, SoftBoxLayout.TOP));
        }
        public function get cellValue():UpgradeDefinition{
            return (this.m_upgrade);
        }
        override public function setCellValue(_arg1):void{
            this.m_upgrade = (_arg1 as UpgradeDefinition);
            if (this.m_upgrade.oldItemName == null){
                this.m_dummy = true;
            } else {
                this.m_curLevel = BanditUtil.getPoliceStationLevel();
                this.m_requiredLevel = Global.gameSettings.getPoliceStationLevel(this.m_upgrade.newItemName);
                this.m_metRequirements = ((this.m_upgrade.requirements.checkRequirements(null)) || ((this.m_curLevel >= this.m_requiredLevel)));
            };
            this.buildCell();
            this.loadAssets();
        }
        protected function buildCell():void{
            this.m_headerPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            this.m_rewardPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER, 10);
            this.m_claimPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER, 10);
            this.buildBackground();
            if (this.m_dummy == false){
                this.makeHeaderPanel();
                this.makeRewardPanel();
                this.append(this.makeClaimPanel());
            };
            ASwingHelper.prepare(this);
        }
        protected function buildBackground():void{
            var _local1:DisplayObject = (new (CopsDialog.assetDict["police_rewardBG_left_blue"])() as DisplayObject);
            var _local2:DisplayObject = (new (CopsDialog.assetDict["police_rewardBG_left_disabled"])() as DisplayObject);
            var _local3:DisplayObject = (new (CopsDialog.assetDict["police_rewardBG_left_white"])() as DisplayObject);
            var _local4:DisplayObject = (new (CopsDialog.assetDict["police_rewardBG_center_blue"])() as DisplayObject);
            var _local5:DisplayObject = (new (CopsDialog.assetDict["police_rewardBG_center_disabled"])() as DisplayObject);
            var _local6:DisplayObject = (new (CopsDialog.assetDict["police_rewardBG_center_white"])() as DisplayObject);
            var _local7:DisplayObject = (new (CopsDialog.assetDict["police_rewardBG_right_blue"])() as DisplayObject);
            var _local8:DisplayObject = (new (CopsDialog.assetDict["police_rewardBG_right_disabled"])() as DisplayObject);
            var _local9:DisplayObject = (new (CopsDialog.assetDict["police_rewardBG_right_white"])() as DisplayObject);
            var _local10:int = (this.m_position % UpgradesPanel.UPS_PER_PAGE);
            if (_local10 == 1){
                this.m_bg = _local3;
            } else {
                if (_local10 == 2){
                    this.m_bg = _local6;
                } else {
                    if (_local10 == 3){
                        this.m_bg = _local6;
                    } else {
                        this.m_bg = _local9;
                    };
                };
            };
            ASwingHelper.setBackground(this, this.m_bg);
            ASwingHelper.setForcedSize(this, new IntDimension(CELL_WIDTH, CELL_HEIGHT));
        }
        protected function changeHeaderColor():void{
            var _local1:TextField = (this.m_titlePanel.getAsset() as TextField);
            var _local2:TextFormat = _local1.getTextFormat();
            _local2.color = EmbeddedArt.blueTextColor;
            _local1.setTextFormat(_local2);
        }
        protected function makeHeaderPanel():void{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local2:uint = EmbeddedArt.blueTextColor;
            if (!this.m_metRequirements){
                _local2 = EmbeddedArt.lightGrayTextColor;
            };
            var _local3:String = ZLoc.t("Items", (this.m_upgrade.newItemName + "_upgradeName"));
            this.m_titlePanel = ASwingHelper.makeMultilineCapsText(_local3, 130, EmbeddedArt.TITLE_FONT, TextFormatAlign.CENTER, 13, _local2);
            _local1.append(this.m_titlePanel);
            this.m_headerPanel.append(_local1);
            ASwingHelper.setForcedHeight(this.m_headerPanel, 30);
            this.appendAll(ASwingHelper.verticalStrut(5), this.m_headerPanel);
        }
        protected function makeRewardPanel():void{
            var _local1:DisplayObject = (new (CopsDialog.assetDict["police_arrow"])() as DisplayObject);
            this.m_iconHolder = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            ASwingHelper.setForcedHeight(this.m_iconHolder, 85);
            var _local2:AssetPane = new AssetPane(_local1);
            var _local3:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            _local3.append(_local2);
            this.m_rewardHolder = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            ASwingHelper.setForcedHeight(this.m_rewardHolder, 80);
            this.m_rewardPanel.appendAll(this.m_iconHolder, _local3, this.m_rewardHolder);
            ASwingHelper.setForcedWidth(this.m_rewardPanel, CELL_WIDTH);
            this.append(this.m_rewardPanel);
        }
        protected function makeClaimPanel():JPanel{
            var _local2:DisplayObject;
            var _local3:JPanel;
            var _local4:uint;
            var _local5:String;
            var _local6:LocalizationObjectToken;
            var _local7:String;
            var _local8:AssetPane;
            var _local9:AssetPane;
            var _local10:JPanel;
            var _local11:String;
            var _local12:AssetIcon;
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            this.m_curLevel = BanditUtil.getPoliceStationLevel();
            this.m_requiredLevel = Global.gameSettings.getPoliceStationLevel(this.m_upgrade.newItemName);
            if (this.m_curLevel >= this.m_requiredLevel){
                _local2 = (new (CopsDialog.assetDict["police_checkmarkBig"])() as DisplayObject);
                _local1.append(new AssetPane(_local2));
            } else {
                _local3 = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
                _local4 = EmbeddedArt.blueTextColor;
                if (!this.m_metRequirements){
                    _local4 = EmbeddedArt.lightGrayTextColor;
                };
                _local5 = this.m_upgrade.requirements.getRequirementValue(Requirements.REQUIREMENT_SPECIAL_BANDITS);
                _local6 = ZLoc.tk("Dialogs", "Bandit", "", int(_local5));
                _local7 = ZLoc.t("Dialogs", "BanditsRequiredText", {bandit:_local6});
                _local8 = ASwingHelper.makeMultilineCapsText(_local5, 130, EmbeddedArt.TITLE_FONT, TextFormatAlign.CENTER, 13, _local4);
                _local9 = ASwingHelper.makeMultilineText(_local7, 130, EmbeddedArt.DEFAULT_FONT_NAME_BOLD, TextFormatAlign.CENTER, 10, _local4);
                _local3.appendAll(_local8, _local9);
                _local3.append(ASwingHelper.verticalStrut(10));
                _local10 = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
                _local11 = ZLoc.t("Dialogs", "UpgradeButtonText").toUpperCase();
                if (this.m_metRequirements){
                    this.m_upgradeButton = new CustomButton(_local11, null, "GreenButtonUI");
                    this.m_upgradeButton.setFont(ASwingHelper.getTitleFont(13));
                    this.m_upgradeButton.addEventListener(MouseEvent.CLICK, this.onUpgrade, false, 0, true);
                    this.m_upgradeButton.setEnabled(true);
                } else {
                    _local11 = ZLoc.t("Dialogs", "UpgradeCashButtonText", {cash:this.m_upgrade.cashCost});
                    _local12 = new AssetIcon((new EmbeddedArt.icon_cash() as DisplayObject));
                    this.m_upgradeButton = new CustomButton(_local11, _local12, "CashButtonUI");
                    if (Global.localizer.langCode != "en"){
                        this.m_upgradeButton.setFont(new ASFont(EmbeddedArt.TITLE_FONT, 10, false, false, false, EmbeddedArt.advancedFontProps));
                        this.m_upgradeButton.setMargin(new Insets(-3, 0, -3, 2));
                    };
                    this.m_upgradeButton.addEventListener(MouseEvent.CLICK, this.onCashBuy, false, 0, true);
                    if (int(this.m_upgrade.oldItemName.split("_")[2]) != BanditUtil.getPoliceStationLevel()){
                        this.m_upgradeButton.setEnabled(false);
                    };
                };
                _local10.setPreferredWidth(this.m_upgradeButton.getPreferredWidth());
                _local10.append(this.m_upgradeButton);
                _local3.append(_local10);
                _local1.append(_local3);
            };
            this.m_claimPanel.append(_local1);
            ASwingHelper.setForcedWidth(this.m_claimPanel, CELL_WIDTH);
            ASwingHelper.prepare(this.m_claimPanel);
            return (this.m_claimPanel);
        }
        public function enableUpgradeButton():void{
            this.m_upgradeButton.setEnabled(true);
        }
        protected function onUpgrade(_arg1:MouseEvent):void{
            if (BanditManager.upgradeAllStations()){
                GameTransactionManager.addTransaction(new TProcessPoliceUpgrade());
                this.refreshClaimPanel();
            };
        }
        protected function onCashBuy(_arg1:MouseEvent):void{
            var event:* = _arg1;
            if (!LocaleHelper.localSetting.sns.cash_enable){
                return;
            };
            if (Global.player.canBuyCash(this.m_upgrade.cashCost, true)){
                UI.displayMessage(ZLoc.t("Dialogs", "BuyCashUpgradeQuestion", {amount:String(this.m_upgrade.cashCost)}), GenericDialogView.TYPE_YESNO, function (_arg1:GenericPopupEvent):void{
                    if (_arg1.button == GenericDialogView.YES){
                        if (BanditManager.upgradeAllStations(m_upgrade.cashCost, m_upgrade.newItemName)){
                            GameTransactionManager.addTransaction(new TProcessPoliceUpgrade());
                            BanditUtil.logDialogStats(("unlock_" + m_upgrade.newItemName), "station_ui");
                            refreshClaimPanel();
                        };
                    };
                });
            };
        }
        protected function loadAssets():void{
            var _local1:Item;
            var _local2:String;
            if (this.m_dummy == false){
                _local1 = Global.gameSettings.getItemByName(this.m_upgrade.newItemName);
                if (((_local1) && (_local1.icon))){
                    this.m_numAssetLoads++;
                    this.m_iconLoader = LoadingManager.load(_local1.icon, this.onIconLoad, LoadingManager.PRIORITY_HIGH);
                };
                _local2 = Global.gameSettings.getPoliceStationNewVehicle(Global.gameSettings.getPoliceStationLevel(this.m_upgrade.newItemName));
                _local1 = Global.gameSettings.getItemByName(_local2);
                if (((_local1) && (_local1.icon))){
                    this.m_numAssetLoads++;
                    this.m_rewardLoader = LoadingManager.load(_local1.icon, this.onIconLoad, LoadingManager.PRIORITY_HIGH);
                };
            };
        }
        override protected function onIconLoad(_arg1:Event):void{
            if (this.m_numAssetLoads > 1){
                this.m_numAssetLoads--;
                return;
            };
            if (this.m_iconLoader){
                this.m_icon = this.m_iconLoader;
            };
            if (((this.m_rewardLoader) && (this.m_rewardLoader.content))){
                this.m_rewardIcon = this.m_rewardLoader.content;
            };
            this.placeIcons();
            setGridListCellStatus(m_gridList, false, m_index);
        }
        protected function placeIcons():void{
            if (this.m_icon){
                this.m_iconHolder.append(new AssetPane(this.m_icon));
            };
            if (this.m_rewardIcon){
                this.m_rewardHolder.append(new AssetPane(this.m_rewardIcon));
            };
            ASwingHelper.prepare(this);
        }
        public function refreshClaimPanel():void{
            this.m_claimPanel.removeAll();
            this.makeClaimPanel();
            this.changeHeaderColor();
            dispatchEvent(new UIEvent(UIEvent.CHANGE_CREW_STATE, "", true));
        }

    }
}//package Display.CopsBanditsUI 
